package com.boyuan.huijiaoyu;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import cn.jpush.android.api.JPushInterface;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.helper.DialogHelper;
import com.magicsoft.app.helper.ScreenManager;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseFragmentActivity extends FragmentActivity {

	// 加载中...显示框
	private Dialog loading;
	// 按返回键退出，默认为不允许退出，若允许退出则在backClicked()中将isBackAllowed = true
	public Boolean isBackAllowed = false;
	// 是否后台进入前台
	public boolean isBackIntoTheFrontDesk = false;

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);
		if (this.getClass() != MainActivity.class) {

			if (ColorHolderApplication.getInstance().mainActivity != null) {

				ColorHolderApplication.getInstance().mainActivity.isBackIntoTheFrontDesk = isBackIntoTheFrontDesk;

				// ColorHolderApplication.getInstance().mainActivity
				// .backendBecomeActive();
			}
		}
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// remove掉保存的Fragment
		// String FRAGMENTS_TAG = "android:support:fragments";
		// savedInstanceState.remove(FRAGMENTS_TAG);

		ScreenManager.getScreenManager().pushActivity(this);

		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		if (ColorHolderApplication.getInstance().mainActivity != null) {

			isBackIntoTheFrontDesk = false;

		}
	}

	public void showLoading(String msg) {

		if (!isFinishing()) {// Activity是否还存在

			if (loading == null) {
				loading = DialogHelper.createLoadingDialog(
						BaseFragmentActivity.this, msg);
			}

			loading.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_BACK
							&& event.getRepeatCount() == 0) {

						// TODO:按返回键取消loading事件
						onBackPressed();

					}
					return false;
				}
			});

			loading.show();
		}

	}

	public void hideLoading() {

		if (loading != null) {
			loading.dismiss();
			loading = null;
		}

	}

	public Boolean isShowingLoading() {

		if (loading != null) {
			return loading.isShowing();
		}

		return false;

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (!isAppOnForeground()) {

			isBackIntoTheFrontDesk = true;
		}
	}

	/**
	 * 程序是否在前台运行
	 * 
	 * @return
	 */
	public boolean isAppOnForeground() {
		// Returns a list of application processes that are running on the
		// device

		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;

		for (RunningAppProcessInfo appProcess : appProcesses) {
			// The name of the process that this object is associated with.
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}

		return false;
	}

	@Override
	public void finish() {

		ScreenManager.getScreenManager().removeActivity(this);

		super.finish();

		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		hideLoading();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(this);
	}

	/**
	 * 返回事件
	 */
	abstract void backClicked();

	@Override
	public void onBackPressed() {

		backClicked();
		if (isBackAllowed) {
			super.onBackPressed();
		}
	}

}
